package cn.yunyichina.provider.base.test.order;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;

import cn.yunyichina.provider.base.dao.OrderDao;
import cn.yunyichina.provider.base.entity.Order;
import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.RefundClinicInsuranceVo;
import cn.yunyichina.provider.iface.entity.hisiface.clinic.RefundClinicInsuranceZfqrVo;

public class RefundOrder {

	static ClassPathXmlApplicationContext applicationContext=null;
	
	static{
		applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		applicationContext.start();	
	}

	
	@Test
	public void refundMedicareOrder(){
		 File file = new File("C:\\Temp\\order.txt");
		 List<String> orders=new ArrayList<String>();
		 if(file.exists()){
			//读出所有的订单号
			BufferedReader reader = null;
	        try {
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            while ((tempString = reader.readLine()) != null) {
	                System.out.println("line " + line + ": " + tempString);
	                orders.add(tempString.trim());
	                line++;
	            }
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
	        System.out.println("需要退款的订单号个数为:"+orders.size());
	        String result="";
	        
	        FileOutputStream out;
			try {
				out = new FileOutputStream("D:/result.txt");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
            PrintStream p=new PrintStream(out);
	        
	        for(String orderNo:orders){
	        	OrderDao orderDao=(OrderDao)applicationContext.getBean("orderDao");
	 	        Order wheres=new Order();
	 	        wheres.setOrderNo(orderNo);
	 	        List<Order> searchOrders=orderDao.select(wheres);
	 	        if(!CollectionUtils.isEmpty(searchOrders)){
	 	        	Order order=searchOrders.get(0);
	 	        	if(order.getIsInsurance()==1){
	 	        		try{
	 	        			RefundClinicInsuranceVo vo= new RefundClinicInsuranceVo();
		 	        		vo.setHospitalCode(order.getOrgCode());
		 	        		vo.setBranchCode("");
		 	        		vo.setDeptCode(order.getDeptCode());
		 	        		vo.setDoctorCode(order.getDoctorCode());
		 	        		vo.setPatCardNo(order.getMedicalCardNo());
		 	        		vo.setPatCardType(String.valueOf(order.getMedicalCardType()));
		 	        		vo.setYytOrdNum(order.getOrderNo());
		 	        		Map<String,String> dataMap=JSON.parseObject(order.getExtraParams(),Map.class);
		 	        		vo.setCancelBillNo(dataMap.get("cancel_bill_no"));
		 	        		vo.setCancelSerialNo(dataMap.get("cancel_serial_no"));
		 	        		vo.setMzFeeId(dataMap.get("mz_fee_id"));
		 	        		vo.setMzBillId(dataMap.get("mz_bill_id"));
		 	        		vo.setSsBillNo(dataMap.get("ss_bill_no"));
		 	        		vo.setSsFeeNo(dataMap.get("ss_fee_no"));
		 	        		ResCommon<Void> response=	HisifaceService.getHisifaceClient().refundClinicInsurance(vo);
		 	        		if("0".equals(response.getResultCode())){
		 	        			result=order.getOrderNo()+"->医保退费成功!";
		 	        			System.out.println(result);
		 	        			p.println(result);
		 	        		}else{
		 	        			result=order.getOrderNo()+"->医保退费失败:"+response.getResultMessage();
		 	        			System.out.println(result);
		 	        			p.println(result);
		 	        		}
	 	        		}catch(Exception ex){
	 	        			result=order.getOrderNo()+"->医保退费异常:"+ex.getMessage();
	 	        			System.out.println(result);
	 	        			p.println(result);
	 	        		}
	 	        		
	 	        	}else{
	 	        		result=order.getOrderNo()+"->自费不需要做医保退款!";
	 	        		System.out.println(result);
	 	        		p.println(result);
	 	        	}
	 	        }
	        }
	        
	        try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	        p.close();
		 }
	       
	}
	
	@Test
	public void refundMedicareOrder2(){
		 File file = new File("C:\\Temp\\order2.txt");
		 List<String> orders=new ArrayList<String>();
		 if(file.exists()){
			//读出所有的订单号
			BufferedReader reader = null;
	        try {
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            while ((tempString = reader.readLine()) != null) {
	                System.out.println("line " + line + ": " + tempString);
	                orders.add(tempString.trim());
	                line++;
	            }
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
	        System.out.println("需要退款的订单号个数为:"+orders.size());
	        String result="";
	        
	        FileOutputStream out;
			try {
				out = new FileOutputStream("D:/result2.txt");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}
            PrintStream p=new PrintStream(out);
	        
	        for(String orderNo:orders){
	        	OrderDao orderDao=(OrderDao)applicationContext.getBean("orderDao");
	 	        Order wheres=new Order();
	 	        wheres.setOrderNo(orderNo);
	 	        List<Order> searchOrders=orderDao.select(wheres);
	 	        if(!CollectionUtils.isEmpty(searchOrders)){
	 	        	Order order=searchOrders.get(0);
	 	        	if(order.getIsInsurance()==1){
	 	        		try{
	 	        			RefundClinicInsuranceZfqrVo vo= new RefundClinicInsuranceZfqrVo();
		 	        		vo.setHospitalCode(order.getOrgCode());
		 	        		vo.setBranchCode("");
		 	        		vo.setDeptCode(order.getDeptCode());
		 	        		vo.setDoctorCode(order.getDoctorCode());
		 	        		vo.setPatCardNo(order.getMedicalCardNo());
		 	        		vo.setPatCardType(String.valueOf(order.getMedicalCardType()));
		 	        		vo.setYytOrdNum(order.getOrderNo());
		 	        		Map<String,String> dataMap=JSON.parseObject(order.getExtraParams(),Map.class);
		 	        		vo.setCancelBillNo(dataMap.get("cancel_bill_no"));
		 	        		vo.setCancelSerialNo(dataMap.get("cancel_serial_no"));
		 	        		vo.setMzFeeId(dataMap.get("mz_fee_id"));
		 	        		vo.setMzBillId(dataMap.get("mz_bill_id"));
		 	        		vo.setSsBillNo(dataMap.get("ss_bill_no"));
		 	        		vo.setSsFeeNo(dataMap.get("ss_fee_no"));
		 	        		vo.setPayAmount(order.getPayAmount().toString());
		 	        		ResCommon<Void> response=	HisifaceService.getHisifaceClient().refundClinicInsuranceZfqr(vo);
		 	        		if("0".equals(response.getResultCode())){
		 	        			result=order.getOrderNo()+"->医保确认成功!";
		 	        			System.out.println(result);
		 	        			p.println(result);
		 	        		}else{
		 	        			result=order.getOrderNo()+"->医保确认退费失败:"+response.getResultMessage();
		 	        			System.out.println(result);
		 	        			p.println(result);
		 	        		}
	 	        		}catch(Exception ex){
	 	        			result=order.getOrderNo()+"->医保确认退费异常:"+ex.getMessage();
	 	        			System.out.println(result);
	 	        			p.println(result);
	 	        		}
	 	        		
	 	        	}else{
	 	        		result=order.getOrderNo()+"->自费不需要做医保退款!";
	 	        		System.out.println(result);
	 	        		p.println(result);
	 	        	}
	 	        }
	        }
	        
	        try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	        p.close();
		 }
	}
	
}
